package com.liushuang.waiwaiim.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liushuang.waiwaiim.model.bean.UserInfo;
import com.liushuang.waiwaiim.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

//联系人表的操作类
public class ContactTableDao {

    private DBHelper mDBHelper;

    public ContactTableDao(DBHelper dbHelper){
        mDBHelper = dbHelper;
    }

    //获取所有的联系人
    public List<UserInfo> getContacts(){

        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        String sql = "select * from " + ContactTable.TABLE_NAME + " where " + ContactTable.COL_IS_CONTACT + "=1";
        Cursor cursor = db.rawQuery(sql, null);

        List<UserInfo> contacts = new ArrayList<>();
        if (cursor != null){
            while (cursor.moveToNext()){
                UserInfo userInfo = new UserInfo();
                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)));
                userInfo.setPhoto(cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHOTO)));

                contacts.add(userInfo);
            }
        }

        cursor.close();
        db.close();

        return contacts;
    }

    // 通过环信id获取联系人单个信息
    public UserInfo getContactByHx(String hxId){

        if (hxId == null){
            return null;
        }

        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        String sql = "select * from " + ContactTable.TABLE_NAME + " where " + ContactTable.COL_HXID + "=?";
        Cursor cursor = db.rawQuery(sql, new String[]{hxId});

        UserInfo userInfo = null;

        if (cursor != null){
            if (cursor.moveToNext()){
                userInfo = new UserInfo();
                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)));
                userInfo.setPhoto(cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHOTO)));

            }
        }

        cursor.close();
        db.close();

        return userInfo;
    }

    // 通过环信id获取用户联系人信息
    public List<UserInfo> getContactsByHx(List<String> hxIds){

        if (hxIds == null || hxIds.size() <= 0){
            return null;
        }

        //遍历hxIds来进行查询
        List<UserInfo> contacts = new ArrayList<>();
        for (String hxId : hxIds) {
            UserInfo contact = getContactByHx(hxId);

            contacts.add(contact);
        }

        return contacts;
    }

    //保存单个联系人
    public void saveContact(UserInfo userInfo, boolean isMyContact){
        if (userInfo == null){
            return;
        }

        SQLiteDatabase db = mDBHelper.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(ContactTable.COL_HXID, userInfo.getHxid());
        values.put(ContactTable.COL_NAME, userInfo.getName());
        values.put(ContactTable.COL_NICK, userInfo.getNick());
        values.put(ContactTable.COL_PHOTO, userInfo.getPhoto());
        values.put(ContactTable.COL_IS_CONTACT, isMyContact ? 1 : 0);

        db.replace(ContactTable.TABLE_NAME, null, values);
        db.close();
    }

    public void saveContacts(List<UserInfo> contacts, boolean isMyContact){

        if (contacts == null || contacts.size() <= 0){
            return;
        }

        for (UserInfo contact : contacts) {
            saveContact(contact, isMyContact);
        }
    }

    //删除联系人信息
    public void deleteContactByHxId(String hxId){

        if (hxId == null){
            return;
        }

        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        db.delete(ContactTable.TABLE_NAME, ContactTable.COL_HXID + "=?", new String[]{hxId});

        db.close();
    }
}
